Radar Host Communication library  2020.08.13
EndpointRadarBaseBoard.c
Go to the documentation of this file.
1 
10 /* ===========================================================================
11 ** Copyright (C) 2017-2021 Infineon Technologies AG
12 ** All rights reserved.
13 ** ===========================================================================
14 **
15 ** ===========================================================================
16 ** This document contains proprietary information of Infineon Technologies AG.
17 ** Passing on and copying of this document, and communication of its contents
18 ** is not permitted without Infineon's prior written authorisation.
19 ** ===========================================================================
20 */
21 
22 /*
23 ==============================================================================
24  1. INCLUDE FILES
25 ==============================================================================
26 */
27 #include "EndpointRadarBaseBoard.h"
28 #define __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__
29 #include "Protocol_internal.h"
30 #undef __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
35 
36 /*
37 ==============================================================================
38  2. LOCAL DEFINITIONS
39 ==============================================================================
40 */
41 
54 #define MSG_SET_DRIVER_VERSION 0x21
55 #define MSG_GET_DEVICE_INFO 0x22
57 #define MSG_CONSUMPTION_DEF_REQ 0xD0
58 #define MSG_CONSUMPTION_DEF_CNF 0xD1
59 
60 #define MSG_CONSUMPTION_REQ 0xD2
61 #define MSG_CONSUMPTION_CNF 0xD3
62 
63 #define MSG_BOARD_INFO_REQ 0xE0
64 #define MSG_BOARD_INFO_CNF 0xE1
65 #define MSG_PARA_STORE_REQ 0xE2
66 #define MSG_PARA_STORE_CNF 0xE3
67 #define MSG_PARA_FACTORY_RESET_REQ 0xE4
68 #define MSG_PARA_FACTORY_RESET_CNF 0xE5
69 
70 #define MSG_ERROR_IND 0xF0
71 
74 /*
75 ==============================================================================
76  5. LOCAL FUNCTION PROTOTYPES
77 ==============================================================================
78 */
83 extern const char* ep_radar_get_error_code_description(uint16_t error_code);
84 
100 static void parse_payload(int32_t protocol_handle, uint8_t endpoint,
101  const uint8_t* payload, uint16_t payload_size);
102 
103 /*
104 ==============================================================================
105  4. DATA
106 ==============================================================================
107 */
121 const Endpoint_Definition_t ep_RadarBaseBoard_definition =
122 {
123  /*.type =*/ 0x544A4241, /* ASCII code 'TJBA' = Tjuefire Mcu base */
124  /*.min_version =*/ 1,
125  /*.max_version =*/ 1,
126  /*.description =*/ "ifxRadar mcu base",
127  /*.parse_payload =*/ parse_payload,
128  /*.get_status_descr =*/ ep_radar_get_error_code_description
129 };
130 
149 static consumption_def_cnf_callback_t consumption_def_cnf_cb = NULL;
150 static consumption_def_cnf_ctx_t *consumption_def_cnf_ctx = NULL;
151 
152 static consumption_cnf_callback_t consumption_cnf_cb = NULL;
153 static consumption_cnf_ctx_t *consumption_cnf_ctx = NULL;
154 
155 static board_info_cnf_callback_t board_info_cnf_cb = NULL;
156 static board_info_cnf_ctx_t *board_info_cnf_ctx = NULL;
157 
158 static para_factory_reset_cnf_callback_t para_factory_reset_cnf_cb = NULL;
159 static para_factory_reset_cnf_ctx_t *para_factory_reset_cnf_ctx = NULL;
160 
161 static para_store_cnf_callback_t para_store_cnf_cb = NULL;
162 static para_store_cnf_ctx_t *para_store_cnf_ctx = NULL;
163 
164 
167 /*
168  * The max str. len that is transmitted is only 255 bytes (because a uint8_t is used for the size
169  *
170  */
171 static uint16_t protocol_read_helper_str_list(const uint8_t *payload, const size_t offset, size_t *p_elements, char ***ptr )
172 {
173  uint16_t increment = 0;
174  uint8_t elements = protocol_read_payload_uint8( &payload[0], (uint16_t)offset );
175  increment += 1;
176  if( p_elements != NULL)
177  *p_elements = (size_t) elements;
178 
179  if( ptr == NULL)
180  return 0;
181  char **pData = (char **) malloc(sizeof(char**) * elements );
182  for( int i = 0; i < elements; i++)
183  {
184  uint8_t len = protocol_read_payload_uint8( &payload[0], (uint16_t)offset + increment );
185  increment += 1;
186  char* pTemp = (char *) malloc(sizeof(char) * ( len + 1 ) );
187  memcpy(pTemp, &payload[offset + increment], len);
188  *(pTemp + len) = 0; // zero terminate the string!
189  pData[i] = pTemp;
190  increment += len;
191  }
192  *ptr = pData;
193  return increment;
194 }
195 
196 
197 /*
198 ==============================================================================
199  6. LOCAL FUNCTIONS
200 ==============================================================================
201 */
202 
203 static int32_t parse_para_values_cnf(int32_t protocol_handle, uint8_t endpoint,
204  const uint8_t* payload, uint16_t payload_size)
205 {
206  uint8_t payloadId = protocol_read_payload_uint8(payload, 0);
207 
208  if ((payloadId == MSG_CONSUMPTION_DEF_CNF) &&
209  (payload_size > 1))
210  {
211  if (consumption_def_cnf_cb)
212  {
213  // TODO: move this out into a seperate function!
214  consumption_def_host_t consumption_def;
215  uint16_t offset = 1; // skip message id
216 
217  uint16_t size = protocol_read_payload_uint16(&payload[0], offset);
218  offset += 2;
219 
220  offset += protocol_read_helper_str_list( &payload[0], offset, &consumption_def.consumption_label_elements, &consumption_def.pp_consumption_labels);
221 
222  // check if offset and size matches!!
223  if( size != offset )
225 
226  /* send frame format to callback */
227  consumption_def_cnf_cb(consumption_def_cnf_ctx, protocol_handle,
228  endpoint, &consumption_def);
229 
230  // free-up memory!!
231  for( size_t i = 0; i < consumption_def.consumption_label_elements; i++)
232  {
233  free(consumption_def.pp_consumption_labels[i]);
234  }
235  free(consumption_def.pp_consumption_labels);
236 
237 
238  }
239  return 1;
240  }
241  else if ((payloadId == MSG_CONSUMPTION_CNF) &&
242  (payload_size > 1))
243  {
244  if (consumption_cnf_cb)
245  {
246 
247  consumption_cnf_t consumption;
248 
249  uint16_t offset = 1;
250  size_t elements = 0;
251  uint16_t size = protocol_read_payload_uint16(&payload[0], offset);
252  offset += sizeof(uint16_t);
253 
254  elements = (size_t) protocol_read_payload_uint8(&payload[0], offset);
255  offset += sizeof(uint8_t);
256 
257  consumption_t *pp_temp = (consumption_t *) malloc( elements *sizeof(consumption_t) );
258 
259  for(uint32_t i = 0; i < elements; i++)
260  {
261  consumption_t *pData = (pp_temp+i);
262  pData->index = protocol_read_payload_uint8(&payload[0], offset);
263  offset += 1;
264  uint32_t temp;
265  temp = protocol_read_payload_uint32(&payload[0], offset);
266  memcpy(&pData->value, &temp, sizeof(float));
267  offset += sizeof(uint32_t);
268  size_t len = (size_t) protocol_read_payload_uint8(&payload[0], offset);
269  offset += 1;
270 
271  pData->p_unit = (char *)malloc(len + 1);
272  memcpy(pData->p_unit, &payload[offset], len);
273  pData->p_unit[len] = 0;
274 
275  offset += (uint16_t)len;
276 
277  }
278  consumption.elements = elements;
279  consumption.p_consumption = pp_temp;
280 
281 
282  if (size != offset)
284 
285  /* send result to callback */
286  consumption_cnf_cb(consumption_cnf_ctx, protocol_handle,
287  endpoint, &consumption);
288 
289  // free the memory again:
290  for( uint32_t i=0; i< elements; i++)
291  {
292  consumption_t *pData = consumption.p_consumption+i;
293  free(pData->p_unit);
294  }
295  free(consumption.p_consumption);
296 
297  }
298  return 1;
299  }
300  else if ((payloadId == MSG_BOARD_INFO_CNF) &&
301  (payload_size > 1))
302  {
303  if (board_info_cnf_cb)
304  {
305  board_info_cnf_t board_info;
306  uint16_t offset = 1;
307  board_info.major_version = protocol_read_payload_uint8(&payload[0], offset);
308  offset += 1;
309  board_info.minor_version = protocol_read_payload_uint8(&payload[0], offset);
310  offset += 1;
311 
312  uint8_t len = protocol_read_payload_uint8(&payload[0], offset);
313  offset += 1;
314  board_info.rf_shield_type_id = (char *)malloc(len+1);
315  memcpy(board_info.rf_shield_type_id, &payload[offset], len);
316  offset += len;
317  board_info.rf_shield_type_id[len] = 0;
318 
319  len = protocol_read_payload_uint8(&payload[0], offset);
320  offset += 1;
321  board_info.description = (char *)malloc(len+1);
322  memcpy(board_info.description, &payload[offset], len);
323  board_info.description[len] = 0;
324  offset += len;
325 
326  /* send result to callback */
327  board_info_cnf_cb(board_info_cnf_ctx, protocol_handle,
328  endpoint, &board_info);
329 
330  free( board_info.rf_shield_type_id);
331  free( board_info.description );
332  }
333 
334  }
335  else if ((payloadId == MSG_PARA_STORE_CNF) &&
336  (payload_size == 1))
337  {
338  if (para_store_cnf_cb)
339  {
340  para_store_cnf_t data;
341  /* send result to callback */
342  para_store_cnf_cb(para_store_cnf_ctx, protocol_handle, endpoint, &data);
343  }
344  }
345 
346  else if ((payloadId == MSG_PARA_FACTORY_RESET_CNF) &&
347  (payload_size == 1))
348  {
349  if (para_factory_reset_cnf_cb)
350  {
352  /* send result to callback */
353  para_factory_reset_cnf_cb(para_factory_reset_cnf_ctx, protocol_handle, endpoint, &data);
354  }
355  }
356 
357  else
358  {
359  printf("tjba: message id not found\n");
360  }
361 
362  return 0;
363 }
364 
365 
366 static void parse_payload(int32_t protocol_handle, uint8_t endpoint,
367  const uint8_t* payload, uint16_t payload_size)
368 {
369  /* try to parse payload for all supported message types, stop when parsing
370  * was successful
371  */
372  if (parse_para_values_cnf(protocol_handle, endpoint,
373  payload, payload_size))
374  return;
375 }
376 
377 /*
378 ==============================================================================
379  7. EXPORTED FUNCTIONS
380 ==============================================================================
381 */
382 
383 int32_t ep_radar_base_board_is_compatible_endpoint(int32_t protocol_handle,
384  uint8_t endpoint)
385 {
386  return protocol_is_endpoint_compatible(protocol_handle, endpoint,
388 }
389 
390 //----------------------------------------------------------------------------
391 //----------------------------------------------------------------------------
392 
393 
394 
395 
396 
397 int32_t ep_radar_base_board_para_factory_reset_req(int32_t protocol_handle, uint8_t endpoint)
398 {
399  /* build message to send */
400  uint8_t cmd_message[1];
401 
402  protocol_write_payload_uint8(cmd_message, 0, MSG_PARA_FACTORY_RESET_REQ);
403 
404  /* send message and process received response */
405  return protocol_send_and_receive(protocol_handle, endpoint,
407  cmd_message, sizeof(cmd_message));
408 }
409 
410 
411 int32_t ep_radar_base_board_para_store_req(int32_t protocol_handle, uint8_t endpoint)
412 {
413  /* build message to send */
414  uint8_t cmd_message[1];
415 
416  protocol_write_payload_uint8(cmd_message, 0, MSG_PARA_STORE_REQ);
417 
418  /* send message and process received response */
419  return protocol_send_and_receive(protocol_handle, endpoint,
421  cmd_message, sizeof(cmd_message));
422 }
423 
424 
425 int32_t ep_radar_base_board_board_info_req(int32_t protocol_handle, uint8_t endpoint)
426 {
427  /* build message to send */
428  uint8_t cmd_message[1];
429 
430  protocol_write_payload_uint8(cmd_message, 0, MSG_BOARD_INFO_REQ);
431 
432  /* send message and process received response */
433  return protocol_send_and_receive(protocol_handle, endpoint,
435  cmd_message, sizeof(cmd_message));
436 }
437 
439  board_info_cnf_ctx_t* p_context)
440 {
441  board_info_cnf_cb = callback;
442  board_info_cnf_ctx = p_context;
443 }
444 
446  para_factory_reset_cnf_ctx_t* p_context)
447 {
448  para_factory_reset_cnf_cb = callback;
449  para_factory_reset_cnf_ctx = p_context;
450 }
451 
453  para_store_cnf_ctx_t* p_context)
454 {
455  para_store_cnf_cb = callback;
456  para_store_cnf_ctx = p_context;
457 }
458 
459 
460 int32_t ep_radar_base_board_consumption_def_req(int32_t protocol_handle, uint8_t endpoint)
461 {
462  /* build message to send */
463  uint8_t cmd_message[1];
464 
465  protocol_write_payload_uint8(cmd_message, 0, MSG_CONSUMPTION_DEF_REQ);
466 
467  /* send message and process received response */
468  return protocol_send_and_receive(protocol_handle, endpoint,
470  cmd_message, sizeof(cmd_message));
471 }
472 
474  consumption_def_cnf_ctx_t* p_context)
475 {
476  consumption_def_cnf_cb = callback;
477  consumption_def_cnf_ctx = p_context;
478 }
479 
480 
481 /*
482  * if repeat_count is 0xFFFF.FFFF the results will be continuously provided, unless stopped (with repeat_count set to 0)
483  * if repeat_count is 1, only a single result is returned, and the next result needs to be retriggerd
484  * in case you like to avoid extra latency, by the round trip request, set the result cnt to 0xfffffffff, and a continuous stream of results will be received.
485  */
486 
487 int32_t ep_radar_base_board_consumption_req(int32_t protocol_handle, uint8_t endpoint, uint32_t repeat_count)
488 {
489  /* build message to send */
490  uint8_t cmd_message[1+sizeof(uint32_t)];
491  uint16_t offset = 0;
492 
493  offset += protocol_write_payload_uint8(cmd_message, offset, MSG_CONSUMPTION_REQ);
494  offset += protocol_write_payload_uint32(cmd_message, offset, repeat_count);
495 
496  /* send message and process received response */
497  return protocol_send_and_receive(protocol_handle, endpoint,
499  cmd_message, offset);
500 }
501 
503  consumption_cnf_ctx_t* p_context)
504 {
505  consumption_cnf_cb = callback;
506  consumption_cnf_ctx = p_context;
507 }
508 
509 
510 
511 
512 
513 
514 
515 
516 /* --- End of File -------------------------------------------------------- */
#define MSG_CONSUMPTION_REQ
void ep_radar_base_board_consumption_def_cb(consumption_def_cnf_callback_t callback, consumption_def_cnf_ctx_t *p_context)
int32_t ep_radar_base_board_para_store_req(int32_t protocol_handle, uint8_t endpoint)
This file contains the API methods applicable generically to Industrial radar Baseboard based Demos.
void ep_radar_base_board_para_store_cb(para_store_cnf_callback_t callback, para_store_cnf_ctx_t *p_context)
#define MSG_BOARD_INFO_CNF
#define MSG_PARA_FACTORY_RESET_CNF
#define MSG_CONSUMPTION_DEF_REQ
void ep_radar_base_board_board_info_cb(board_info_cnf_callback_t callback, board_info_cnf_ctx_t *p_context)
This file defines the status codes of the radar endpoints.
#define MSG_BOARD_INFO_REQ
void ep_radar_base_board_para_factory_reset_cb(para_factory_reset_cnf_callback_t callback, para_factory_reset_cnf_ctx_t *p_context)
void(* board_info_cnf_callback_t)(board_info_cnf_ctx_t *p_context, int32_t protocol_handle, uint8_t endpoint, board_info_cnf_t *p_data)
#define EP_RADAR_ERR_MSG_SIZE_CORRUPT
int32_t protocol_send_and_receive(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton, const uint8_t *payload, uint16_t payload_size)
Definition: Protocol.c:957
const char * ep_radar_get_error_code_description(uint16_t error_code)
int32_t ep_radar_base_board_is_compatible_endpoint(int32_t protocol_handle, uint8_t endpoint)
This function checks if an endpoint in a device is a Radar Baseboard endpoint.
void(* para_store_cnf_callback_t)(para_store_cnf_ctx_t *p_context, int32_t protocol_handle, uint8_t endpoint, para_store_cnf_t *p_data)
int32_t ep_radar_base_board_consumption_req(int32_t protocol_handle, uint8_t endpoint, uint32_t repeat_count)
#define MSG_CONSUMPTION_CNF
int32_t protocol_is_endpoint_compatible(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton)
Definition: Protocol.c:927
consumption_t * p_consumption
const Endpoint_Definition_t ep_RadarBaseBoard_definition
#define MSG_PARA_FACTORY_RESET_REQ
void ep_radar_base_board_consumption_cb(consumption_cnf_callback_t callback, consumption_cnf_ctx_t *p_context)
int32_t ep_radar_base_board_board_info_req(int32_t protocol_handle, uint8_t endpoint)
void(* consumption_def_cnf_callback_t)(consumption_def_cnf_ctx_t *p_context, int32_t protocol_handle, uint8_t endpoint, consumption_def_host_t *p_data)
This is the callback type executed on a parameter value confirmation event, triggered,...
void(* consumption_cnf_callback_t)(consumption_cnf_ctx_t *p_context, int32_t protocol_handle, uint8_t endpoint, consumption_cnf_t *p_data)
#define MSG_CONSUMPTION_DEF_CNF
int32_t ep_radar_base_board_consumption_def_req(int32_t protocol_handle, uint8_t endpoint)
This function is used to request the parameter values,.
int32_t ep_radar_base_board_para_factory_reset_req(int32_t protocol_handle, uint8_t endpoint)
#define MSG_PARA_STORE_CNF
#define MSG_PARA_STORE_REQ
void(* para_factory_reset_cnf_callback_t)(para_factory_reset_cnf_ctx_t *p_context, int32_t protocol_handle, uint8_t endpoint, para_factory_reset_t *p_data)